package com.xintoucloud.gsv.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.xintoucloud.gsv.entity.MenuRole;
import com.xintoucloud.gsv.entity.Role;
import com.xintoucloud.gsv.model.MenuRoleVo;
import com.xintoucloud.gsv.model.RoleVo;


public interface RoleMapper {
	//页面展示
	@Select("select r.rid,r.rname,r.detail,p.rpid,p.pname,p.detail perDetail,s.rsid,s.state from t_role r join t_role_permission p on(r.permission_id=p.rpid) join t_role_state s on(r.state_id=s.rsid) order by r.rid asc")
	List<RoleVo> listRoleVo();
	
	//根据id查询一个角色
	@Select("select * from t_role where rid=#{rid}")
	@Results({
		@Result(property="rolePermission",column="permission_id",one=@One(select="com.xintoucloud.gsv.mapper.RolePermissionMapper.getOneById")),
		@Result(property="roleState",column="state_id",one=@One(select="com.xintoucloud.gsv.mapper.RoleStateMapper.getOneById"))
	})
	Role getRole(@Param("rid")int rid);

	//查询所有
	@Select("select * from t_role ")
	List<Role> list();

	//查询所有 并根据参数 pageIndex(开始序号)、limit(一页数量) 分页
	@Select("select r.rid,r.rname,r.detail,p.rpid,p.pname,p.detail perDetail,s.rsid,s.state from t_role r join t_role_permission p on(r.permission_id=p.rpid) join t_role_state s on(r.state_id=s.rsid) order by r.rid asc limit #{pageIndex},#{limit};")
	List<RoleVo> listByPageAndLimit(@Param("pageIndex")long pageIndex,@Param("limit")int limit);

	//查询数据 总数 (用于告诉前台需要分几页)
	@Select("select count(*) from t_role ")
	Long count();
	
	//根据id删除一个角色
	@Delete("delete from t_role where rid=#{rid}")
	int roleDelete(@Param("rid")int rid);
	
	//添加一个
	@Insert("insert into t_role values(null,#{rname},#{detail},#{rolePermission.rpid},#{roleState.rsid})")
	@Options(keyProperty = "rid",useGeneratedKeys = true)
	void roleAdd(Role role);
	
	//修改一个角色  参数:role
	@Update("update t_role set rname=#{rname},detail=#{detail},permission_id=#{rolePermission.rpid},state_id=#{roleState.rsid} where rid=#{rid}")
	int roleUpdate(Role role);
	
}
